function mergePDF(fileCell,outName,garbageCollect)
% Function that appends all the PDF files references in fileCell into one
% big pdf labeled outName.pdf. 
% =========================================================================
% [Inputs]
% fileCell      : cell of string names of pdfs to merge
% outName       : string name of merged file
% collectGarbage: Optional arguement.TRUE if you want to delete all of the 
%                 individual files afterwards 
% =========================================================================
% [Example]
% fileCell = {'a','b','c.pdf'};
% outName = 'bigFile'
% appendPDF(fileCell,outName,1)
% =========================================================================
% [Notes]
% Assumes "gswin32.exe" is installed in "C:\Program Files\gs\gs8.64\bin\"
% =========================================================================

garbageCell = fileCell;

for k = 1:length(fileCell)
    
    if isempty( strfind(fileCell{k},'.pdf') )
          garbageCell{k} = [fileCell{k},'.pdf'];
          fileCell{k} = ['"',fileCell{k},'.pdf"'];        
    else 
          fileCell{k} = ['"',fileCell{k},'"'];              
    end
    
end

if isempty( strfind(outName,'.pdf') )
    outName = ['"',outName,'.pdf"'];        
else 
    outName = ['"',outName,'"'];              
end

fileString = [];

for k = 1:length(fileCell)
    
    fileString = [fileString,' ',fileCell{k}];
    
end

str1 = '"C:\Program Files\gs\gs8.64\bin\gswin32.exe" -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=';
str2 = [outName,' -dBatch'];
str3 = fileString;
str4 = ' -c quit';

dosCommand = [str1,str2, str3,str4];

failStatus = dos(dosCommand);

if failStatus
    display('Make sure "gswin32.exe" is installed in "C:\Program Files\gs\gs8.64\bin\"');
end

if nargin == 3
    if garbageCollect
        for k = 1:length(fileCell)
            delete(garbageCell{k}); 
        end 
    end

end

end






